package com.kaaction.modules.mer.api;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.kaaction.common.web.BaseController;
import com.kaaction.modules.mer.service.OperationContentService;
import com.kaaction.modules.mer.service.OperationContentTagService;
import com.kaaction.modules.mer.service.OperationProductService;
import com.kaaction.modules.mer.service.OperationSubjectService;

@Controller
@RequestMapping(value = "/api/v1/OperationApi")
public class OperationApiController extends BaseController{
	
	@Autowired
	OperationContentService operationContentService;
	
	@Autowired
	OperationProductService operationProductService;
	
	@Autowired
	OperationSubjectService operationSubjectService;
	
	@Autowired
	OperationContentTagService operationContentTagService;
	
	
	/**
	 * 首页展示
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 */
	@RequestMapping(value = "/selHomeDisplay ")
	public void selHomeDisplay(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("listBanner", operationContentService.selBanner());//Banner展示
		map.put("listOperProduct", operationProductService.selOperProduct());//精选展示
		map.put("listOperSubject", operationSubjectService.selOperSubject());//专题展示
		super.writeAjaxResponse(response,map);
	}
	
	/**
	 * 某个精选下展示内容
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 */
	@RequestMapping(value = "/selSelectedContentDtl")
	public void selSelectedContentDtl(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException {
		String id = request.getParameter("id");
		super.writeAjaxResponse(response,operationContentService.selSelectedContentDtl(id));
	}
	
	
	/**
	 * 精选展示内容
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 */
	@RequestMapping(value = "/selSelectedContent")
	public void selSelectedContent(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException {
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		int pa = Integer.valueOf(request.getParameter("pageNo"));
		int pageNo = (pa -1)*4;
		int pageSize = 4;
		
		map.put("pageNo", pageNo);
		map.put("pageSize", pageSize);
		List<Map<String,Object>> listSelectedContent = operationContentService.selSelectedContent(map);
		map.clear();
		map.put("listSelectedContent", listSelectedContent);
		map.put("listTag", operationContentTagService.selTag());
		super.writeAjaxResponse(response,map);
	}
	
	
	/**
	 * 根据标签查询精选展示内容
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 */
	@RequestMapping(value = "/selTagContent")
	public void selTagContent(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException {
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		String tagId = request.getParameter("tagId");
		
		int pa = Integer.valueOf(request.getParameter("pageNo"));
		int pageNo = (pa -1)*4;
		int pageSize = 4;
		
		map.put("pageNo", pageNo);
		map.put("pageSize", pageSize);
		map.put("tagId", tagId);
		
		super.writeAjaxResponse(response,operationContentService.selTagContent(map));
	}
	
	
	/**
	 * 某个专题内容显示
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 */
	@RequestMapping(value = "/selOperSubjectContent")
	public void selOperSubjectContent(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException {
		String id = request.getParameter("id");//专题Id
		super.writeAjaxResponse(response,operationSubjectService.selOperSubjectContent(id));
	}
	
	
	/**
	 * 某个小编推荐下展示内容
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 */
	@RequestMapping(value = "/selOperProductDtl")
	public void selOperProductDtl(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException {
		String id = request.getParameter("id");
		super.writeAjaxResponse(response,operationProductService.selOperProductDtl(id));
	}
	
	
}
